<template>
  <div class="mod-config">
    <el-form :inline="true" v-show="searchShow" ref="searchForm" :model="dataForm" @keyup.enter.native="searchChange()">
      <el-form-item label="问卷标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="问卷标题" size="mini"></el-input>
      </el-form-item>
      <el-form-item label="奖励类型" prop="rewardType">
        <el-select v-model="dataForm.rewardType" placeholder="请选择">
          <el-option :value="1" label="积分奖励"></el-option>
          <el-option :value="0" label="无奖励"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="searchChange">查询</el-button>
        <el-button type="default" icon="el-icon-delete" @click="searchReset">清空</el-button>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading">
        <el-table-column prop="title" label="问卷调查标题"></el-table-column>
        <el-table-column prop="rewardType" label="奖励类型">
          <template slot-scope="scope">
            <span v-if="scope.row.rewardType == '1'">无奖励</span>
            <span v-if="scope.row.rewardType == '2'">积分奖励</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="checkDetail(scope.row.id)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <record-form v-if="recordVisible" ref="recordDialog"></record-form>
  </div>
</template>

<script>
  import { fetchWxQuestionnaireList, delWxQuestionnaireObj } from '@/api/mp/wxquestionnaire'
  // import TableForm from './wxquestionnaire-form'
  import { mapGetters } from 'vuex'

  import RecordForm from './RecordForm'
  export default {
    data() {
      return {
        recordVisible: false,
        dataForm: {
          key: ''
        },
        dataList: [],
        searchShow: true,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false
      }
    },
    components: {
      RecordForm
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      init(){
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchWxQuestionnaireList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 查看详情
      checkDetail(id) {
        this.recordVisible = true
        this.$nextTick(() => {
          this.$refs.recordDialog.init(id)
        })
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return delWxQuestionnaireObj(id)
        }).then(data => {
          this.$message.success('删除成功')
          this.getDataList()
        })
      },
      searchChange() {
        this.pageIndex = 1;
        this.getDataList()
      },
      // 搜索清空
      searchReset() {
        this.$refs.searchForm.resetFields();
        this.getDataList(this.page)
      },
      handleSearchShow() {
        this.searchShow = !this.searchShow;
      },
      /**
       * 刷新回调
       */
      refreshChange() {
        this.getDataList(this.page)
      },
      rewardTypeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case 1:
            return '积分奖励'
            break
          case 0:
            return '无奖励'
            break
        }
      },
    }
  }
</script>

<style>
</style>
